<template>
  <div>
    <el-dialog :title="info.isAdd ? '分类添加' : '分类修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="user.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <!-- 遍历arr  -->
            <el-option
              v-for="item in showArr"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="user.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <upload-img @sendFile="user.img=$event" ref="upload"></upload-img>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqcateadd, reqcateinfo, reqcateedit } from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";
export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        pid: "",
        catename: "",
        img: null,
        status: 1
      },
      imgUrl: ""
    };
  },
  computed: {
    ...mapGetters({
      arr: "cate/arr"
    }),
    showArr() {
      return this.arr.filter(item => item.id != this.user.id);
    }
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr: "cate/reqArr"
    }),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.pid === "") {
          errorAlert("上级分类不能为空");
          return;
        }
        if (this.user.catename == "") {
          errorAlert("分类名称不能为空");
          return;
        }
        if (this.user.pid!=0&&(this.user.img == null||this.user.img == "null")) {
          errorAlert("图片不能为空");
          return;
        }
        resolve()
      });
    },
    add() {
      this.checkdata().then(() => {
        reqcateadd(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    },
    empty() {
      this.user = {
        pid: "",
        catename: "",
        img: null,
        status: 1
      };
      this.$refs.upload.setImgUrl("");
    },
    getOne(id) {
      reqcateinfo({ id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.user.id = id;
          this.user.img != "null"
            ? this.$refs.upload.setImgUrl(this.$pre + this.user.img)
            : this.$refs.upload.setImgUrl("");
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        reqcateedit(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @primary-light;
  position: relative;
  h3 {
    text-align: center;
    line-height: 100px;
  }
  input,
  img {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }
  input {
    opacity: 0;
  }
}
</style>